package com.easylive.aspect;

import com.easylive.annotation.RecordUserMessage;
import com.easylive.entity.component.RedisComponent;
import com.easylive.entity.constants.Constants;
import com.easylive.entity.dto.TokenUserInfoDto;
import com.easylive.entity.enums.MessageReadTypeEnum;
import com.easylive.entity.enums.MessageTypeEnum;
import com.easylive.entity.enums.UserActionTypeEnum;
import com.easylive.entity.enums.VideoStatusEnum;
import com.easylive.entity.po.UserAction;
import com.easylive.entity.po.UserMessage;
import com.easylive.entity.po.VideoInfo;
import com.easylive.entity.request.useraction.WebUserActionDoActionReqVO;
import com.easylive.entity.request.videocomment.WebVideoCommentPostCommentReqVO;
import com.easylive.entity.request.videoinfo.AdminVideoInfoAuditVideoReqVO;
import com.easylive.entity.vo.ResponseVO;
import com.easylive.exception.BusinessException;
import com.easylive.service.UserActionService;
import com.easylive.service.UserMessageService;
import com.easylive.service.VideoInfoService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Date;
import java.util.Objects;

/**
 * @Author: yudaxian
 * @DateTime: 2024/12/24 12:33
 **/
@Aspect
@Component
@Slf4j
public class UserMessageOperationAspect {

    @Resource
    private RedisComponent redisComponent;

    @Resource
    private UserMessageService userMessageService;

    @Resource
    private VideoInfoService videoInfoService;

    @Resource
    private UserActionService userActionService;

    /**
     * 通过AOP去返回 responseVO
     * ProceedingJoinPoint 继承 JointPoint
     * @return
     */
    @Around("@annotation(com.easylive.annotation.RecordUserMessage)")
    public ResponseVO aroundInterceptor(ProceedingJoinPoint joinPoint) throws Throwable {

        /**
         * joinPoint.proceed() 返回的就是目标方法的返回值
         */
        ResponseVO responseVO = (ResponseVO) joinPoint.proceed();

        Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
        RecordUserMessage recordUserMessage = method.getAnnotation(RecordUserMessage.class);
        if (recordUserMessage != null) {
            // 定义私有方法保存消息
            saveMessage(recordUserMessage, joinPoint.getArgs(), method.getParameters());
        }

        return responseVO;
    }

    /**
     *
     * @param recordUserMessage
     * @param args 参数值
     * @param parameters 参数名
     */
    private void saveMessage(RecordUserMessage recordUserMessage, Object[] args, Parameter[] parameters) {
        Object object = args[0];
        Class<?> type = parameters[0].getType();

        /**
         * 不同的参数
         */
        String videoId = null;
        // 回复
        Integer replyCommentId = null;
        String content = null;

        MessageTypeEnum messageTypeEnum = recordUserMessage.messageType();

        // 判断参数类型
        if (type.equals(AdminVideoInfoAuditVideoReqVO.class)) {
            AdminVideoInfoAuditVideoReqVO adminVideoInfoAuditVideoReqVO = (AdminVideoInfoAuditVideoReqVO) object;
            videoId = adminVideoInfoAuditVideoReqVO.getVideoId();
            if (Objects.equals(adminVideoInfoAuditVideoReqVO.getStatus(), VideoStatusEnum.STATUS4.getStatus())) {
                content = adminVideoInfoAuditVideoReqVO.getReason();
            }
        } else if (type.equals(WebUserActionDoActionReqVO.class)) {
            // 判断点赞还是收藏
            WebUserActionDoActionReqVO webUserActionDoActionReqVO = (WebUserActionDoActionReqVO) object;
            videoId = webUserActionDoActionReqVO.getVideoId();
            if (webUserActionDoActionReqVO.getActionType().equals(UserActionTypeEnum.VIDEO_LIKE.getType())) {
                messageTypeEnum = MessageTypeEnum.LIKE;
            } else if (webUserActionDoActionReqVO.getActionType().equals(UserActionTypeEnum.VIDEO_COLLECT.getType())) {
                messageTypeEnum = MessageTypeEnum.COLLECTION;
            }
        } else if (type.equals(WebVideoCommentPostCommentReqVO.class)) {
            WebVideoCommentPostCommentReqVO webVideoCommentPostCommentReqVO = (WebVideoCommentPostCommentReqVO) object;
            videoId = webVideoCommentPostCommentReqVO.getVideoId();
            replyCommentId = webVideoCommentPostCommentReqVO.getReplyCommentId();
            content = webVideoCommentPostCommentReqVO.getContent();
        }

        // 获取token
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader(Constants.COOKIE_TOKEN_WEB);
        TokenUserInfoDto tokenUserInfoDto = redisComponent.getTokenUserInfoDto(token);

        userMessageService.saveUserMessage(videoId, tokenUserInfoDto == null ? null : tokenUserInfoDto.getUserId(), messageTypeEnum, content, replyCommentId);
    }

}
